package com.wman.drawing.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.view.View;

import com.wman.sheep.common.utils.DensityUtil;

import androidx.annotation.Nullable;

/**
 * @ProjectName: ${PROJECT_NAME}
 * @Package: ${PACKAGE_NAME}
 * @ClassName: ${NAME}
 * @Description: java类作用描述
 * @Author: wman
 * @CreateDate: ${DATE} ${TIME}
 * @UpdateUser: 更新者
 * @UpdateDate: ${DATE} ${TIME}
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class TestView extends View {
    /**
     * 反锯齿，原理见 http://hencoder.com
     */
    Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    Path mPath = new Path();
    PathMeasure mPathMeasure;

    public TestView(Context context) {
        super(context);
    }

    public TestView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 每一次layout结束，只有尺寸变化了，才会调用该方法
     *
     * @param w
     * @param h
     * @param oldw
     * @param oldh
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mPath.reset();
        mPath.addRect(getWidth() / 2 - 150, getHeight() / 2 - 300,
                getWidth() / 2 + 150, getHeight() / 2, Path.Direction.CCW);
        mPath.addCircle(getWidth() / 2, getHeight() / 2, 150, Path.Direction.CW);
        mPath.addCircle(getWidth() / 2, getHeight() / 2, 400, Path.Direction.CW);

        //true强制封闭
        mPathMeasure = new PathMeasure(mPath, false);
        //整个路径的长度
        mPathMeasure.getLength();
        //制定某个长度处的坐标和角度
        //mPathMeasure.getPosTan();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawLine(100, 100, 200, 200, mPaint);
        canvas.drawCircle(getWidth() / 2, getHeight() / 5, DensityUtil.dip2px(50), mPaint);
        /**
         * 圆转楼梯，默认是WINDING
         * WINDING 画一条线，同方向穿过+1，反方向穿过-1，为0是外部，不为0 是内部,做镂空就反方向绘图
         * INVERSE_WINDING 与 WINDING相反
         */
        mPath.setFillType(Path.FillType.WINDING);
        /**
         * 镂空就很容易做
         * EVEN_ODD 画一条线，穿过的数量是奇数就是内部，偶数就是外部
         */
        mPath.setFillType(Path.FillType.EVEN_ODD);
        canvas.drawPath(mPath, mPaint);
    }
}
